<script lang="ts" setup>
import {computed, ref} from "vue";
import {message} from "ant-design-vue";
import request from "@/service/request";
import {store} from "@/store";
import router from "@/router";

const pattern = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/;
const loginUser: any = ref({
  loginName: "test",
  password: "test001"
});
const loginModalVisible = ref(false);
const loginModalLoading = ref(false);
const user = computed(() => store.state.user);
const selectedKeys = ref([sessionStorage.getItem("headerItem")||"Home"])
declare let hexMd5: any;
declare let KEY: any;

const headerItemClick = (obj: any) => {
  sessionStorage.setItem("headerItem",obj.key)
};

// 登录
const login = () => {
  if (!pattern.test(loginUser.value.password)) {
    message.error("用户名不存在或密码错误!")
    return
  }
  loginModalLoading.value = true;
  loginUser.value.password = hexMd5(loginUser.value.password + KEY);
  request.post('/user/login', loginUser.value).then((res: any) => {
    loginModalLoading.value = false;
    const data = res.data;
    if (data.success) {
      loginModalVisible.value = false;
      message.success("登录成功！");
      store.commit("setUser", data.content)
    } else {
      message.error(data.message);
    }
  })
};

// 退出登录
const logout = () => {
  request.get('/user/logout/' + user.value.token).then((res: any) => {
    const data = res.data;
    if (data.success) {
      message.success("退出登录成功！")
      store.commit("setUser", {})
      sessionStorage.clear()
    } else {
      message.error(data.message);
    }
  }).then(() => {
    router.push('/')
  })
};

</script>

<template>
  <div class="the-header">
    <a-layout-header class="header">
      <div class="logo"/>
      <a class="login" v-show="!user.id" @click="loginModalVisible = true;">
        <span>登录</span>
      </a>
      <a-popconfirm
          title="确认退出登录?"
          ok-text="是"
          cancel-text="否"
          @confirm="logout()"
      >
        <a class="login" v-show="user.id">
          <span>退出</span>
        </a>
      </a-popconfirm>
      <a class="login" v-show="user.id">
        <span>您好:{{ user.name }}</span>
      </a>
      <a-modal
          title="登录"
          v-model:visible="loginModalVisible"
          :confirm-loading="loginModalLoading"
          @ok="login"
      >
        <a-form :model="loginUser" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
          <a-form-item label="登录名">
            <a-input v-model:value="loginUser.loginName"/>
          </a-form-item>
          <a-form-item label="密码">
            <a-input v-model:value="loginUser.password" type="password"/>
          </a-form-item>
        </a-form>
      </a-modal>
      <a-menu
          theme="dark"
          mode="horizontal"
          v-model:selectedKeys="selectedKeys"
          :style="{ lineHeight: '64px' }"
          @click="headerItemClick"

      >
        <a-menu-item key="Home">
          <router-link to="/">主页</router-link>
        </a-menu-item>
        <a-menu-item key="AdminUser" v-if="user.id">
          <router-link to="/admin/user">用户管理</router-link>
        </a-menu-item>
        <a-menu-item key="AdminEbook" v-if="user.id">
          <router-link to="/admin/ebook">电子书管理</router-link>
        </a-menu-item>
        <a-menu-item key="AdminCategory" v-if="user.id">
          <router-link to="/admin/category">分类管理</router-link>
        </a-menu-item>
        <a-menu-item key="About">
          <router-link to="/about">关于我们</router-link>
        </a-menu-item>
      </a-menu>
    </a-layout-header>
  </div>
</template>

<style lang="less" scoped>

.the-header {
  .header {
    .logo {
      float: left;
      width: 120px;
      height: 31px;
      margin: 16px 24px 16px 0;
      background: rgba(255, 255, 255, 0.3);
    }

    .login {
      float: right;
      color: #fff;
      padding-left: 10px;
    }

    .login:hover {
      color: blueviolet;
    }
  }
}

</style>